package 职责链.Demo3;

// HandlerChain类用数据而非链表来保存所有的处理器，并且需要在HandlerChain的handle()函数中，依次调用每个处理器的handle()函数

import java.util.ArrayList;
import java.util.List;

interface IHandler {
    boolean handle();
}

class HandlerA implements IHandler {

    @Override
    public boolean handle() {
        return false;
    }
}

class HandlerB implements IHandler {

    @Override
    public boolean handle() {
        return false;
    }
}
class HandlerChain {
    private List<IHandler> handlers = new ArrayList<>();

    public void addHandler(IHandler handler) {
        this.handlers.add(handler);
    }

    public void handle() {
        for(IHandler handler: handlers) {
            boolean handled = handler.handle();
            if(handled) {
                break;
            }
        }
    }

}
public class Application {
    public static void main(String[] args) {
        HandlerChain chain = new HandlerChain();
        chain.addHandler(new HandlerA());
        chain.addHandler(new HandlerB());
        chain.handle();
    }
}
